package com.example.device

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.adapter.DeviceOrderAdapter
import com.example.device.databinding.ActivityDeviceOrderBinding
import com.example.device.vm.DeviceOrderViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/device/DeviceOrderActivity")
class DeviceOrderActivity : BaseMvvmActivity<ActivityDeviceOrderBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_device_order
    val deviceOrderAdapter=DeviceOrderAdapter()
    private val deviceOrderViewModel:DeviceOrderViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        mDataBinding.rv.adapter=deviceOrderAdapter
        deviceOrderViewModel.mSuccessLiveData.observe(this){
            deviceOrderAdapter.data.clear()
            deviceOrderAdapter.data.addAll(it.data.reversed())
            deviceOrderAdapter.notifyDataSetChanged()
        }
        deviceOrderViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        //左滑删除
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left=ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val postion=viewHolder.layoutPosition
                deviceOrderAdapter.data.removeAt(postion)
                deviceOrderAdapter.notifyItemRemoved(postion)
            }

        }).attachToRecyclerView(mDataBinding.rv)
        //点击进入详情
        deviceOrderAdapter.setOnItemClickListener { adapter, view, position ->
            val deviceId=deviceOrderAdapter.data.get(position).id
            val inspector=deviceOrderAdapter.data.get(position).inspector
            ARouter.getInstance().build( "/device/DeviceXiangActivity")
                .withInt("deviceId",deviceId)
                .withString("inspector",inspector)
                .navigation()
        }
        mDataBinding.add.setOnClickListener {
            ARouter.getInstance().build("/device/DeviceOrderAddActivity").navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        deviceOrderViewModel.getListDataOrder()
    }
}